﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz.Net.Model;
using System.Data.OracleClient;
using System.Data;

namespace Quartz.Net.DAL
{
    /// <summary>
    /// 任务触发器表
    /// </summary>
   public class Task_Quartz_JobtriggerDAL
    {
       //string connstr = 
       /// <summary>
       /// 插入任务表数据
       /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
       public int InsertJob(Task_Quartz_JobTriggerModel model)
       {
           string strSql = @"insert into TASK_QUARTZ_JOBTRIGGER (NGUID,JOBNAME,JOBGROUPNAME,TRIGGERNAME,TRIGGERGROUPNAME,JOBPATH,JOBCRON,JOBJJ,XH,RUNSTATUS,ISCHANGE,VALID,JOBSNAME) 
                    values 
                    (:NGUID,:JOBNAME,:JOBGROUPNAME,:TRIGGERNAME,:TRIGGERGROUPNAME,:JOBPATH,:JOBCRON,:JOBJJ,:XH,:RUNSTATUS,:ISCHANGE,:VALID,:JOBSNAME)";

           OracleParameter[] para = {
                                    new OracleParameter(":NGUID",OracleType.VarChar,200),
                                    new OracleParameter(":JOBNAME",OracleType.VarChar,500),
                                    new OracleParameter(":JOBGROUPNAME",OracleType.VarChar,500),
                                    new OracleParameter(":TRIGGERNAME",OracleType.VarChar,500),
                                    new OracleParameter(":TRIGGERGROUPNAME",OracleType.VarChar,500),
                                    new OracleParameter(":JOBPATH",OracleType.VarChar,2000),
                                    new OracleParameter(":JOBCRON",OracleType.VarChar,200),
                                    new OracleParameter(":JOBJJ",OracleType.VarChar,2000),
                                    new OracleParameter(":XH",OracleType.Number),
                                    new OracleParameter(":RUNSTATUS",OracleType.Char,1),
                                    new OracleParameter(":ISCHANGE",OracleType.Char,1),
                                    new OracleParameter(":VALID",OracleType.Char,1),
                                    new OracleParameter(":JOBSNAME",OracleType.VarChar,500),
                                    };
           para[0].Value = model.NGUID;
           para[1].Value = model.JOBNAME;
           para[2].Value = model.JOBGROUPNAME;
           para[3].Value = model.TRIGGERNAME;
           para[4].Value = model.TRIGGERGROUPNAME;
           para[5].Value = model.JOBPATH;
           para[6].Value = model.JOBCRON;
           para[7].Value = model.JOBJJ;
           para[8].Value = model.XH;
           para[9].Value = model.RUNSTATUS;
           para[10].Value = model.ISCHANGE;
           para[11].Value = model.VALID;
           para[12].Value = model.JOBSNAME;

          return DalBase.ExecuteNonQuery(strSql, para);

       }

       /// <summary>
       /// 更新任务
       /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
       public int UpdateJob(Task_Quartz_JobTriggerModel model)
       {
           string strSql = "update TASK_QUARTZ_JOBTRIGGER set JOBCRON=:JOBCRON,JOBJJ=:JOBJJ,ISCHANGE=:ISCHANGE,JOBSNAME=:JOBSNAME where nguid = :nguid";

           OracleParameter[] para = {
                                         new OracleParameter(":JOBCRON",OracleType.VarChar,200),
                                         new OracleParameter(":JOBJJ",OracleType.VarChar,2000),
                                         new OracleParameter(":ISCHANGE",OracleType.Char,1),
                                         new OracleParameter(":nguid",OracleType.VarChar,200),
                                         new OracleParameter(":JOBSNAME",OracleType.VarChar,500)
                                     };

           para[0].Value = model.JOBCRON;
           para[1].Value = model.JOBJJ;
           para[2].Value = model.ISCHANGE;
           para[3].Value = model.NGUID;
           para[4].Value = model.JOBSNAME;

           return DalBase.ExecuteNonQuery(strSql, para);
       }

       /// <summary>
       /// 获取当前有效的任务
       /// </summary>
       /// <returns></returns>
       public IEnumerable<Task_Quartz_JobTriggerModel> GetInfo()
       {
           List<Task_Quartz_JobTriggerModel> TASK_QUARTZ_JOBTRIGGER = new List<Task_Quartz_JobTriggerModel>();

           string strSql = "select NGUID,JOBNAME,JOBGROUPNAME,TRIGGERNAME,TRIGGERGROUPNAME,JOBPATH,JOBCRON,JOBJJ,XH,VALID,RUNSTATUS,ISCHANGE,JOBSNAME from  Task_Quartz_JobTrigger ";
          
           DataTable dt = DalBase.ExecuteDataTable(strSql, null);
           foreach (DataRow dr in dt.Rows)
           {
               TASK_QUARTZ_JOBTRIGGER.Add(ToModel(dr));
           }


           return TASK_QUARTZ_JOBTRIGGER;
       }

       /// <summary>
       /// 获取单个任务数据
       /// </summary>
       /// <param name="nguid"></param>
       /// <returns></returns>
       public IEnumerable<Task_Quartz_JobTriggerModel> GetOneInfo(string nguid)
       {
           List<Task_Quartz_JobTriggerModel> TASK_QUARTZ_JOBTRIGGER = new List<Task_Quartz_JobTriggerModel>();

           string strSql = "select NGUID,JOBNAME,JOBGROUPNAME,TRIGGERNAME,TRIGGERGROUPNAME,JOBPATH,JOBCRON,JOBJJ,XH,RUNSTATUS,ISCHANGE,JOBSNAME from Task_Quartz_JobTrigger where nguid=:nguid and valid = '1'";
           OracleParameter[] para = {
                                         new OracleParameter(":nguid",OracleType.VarChar,200)
                                     };
           para[0].Value = nguid;
           DataTable dt = DalBase.ExecuteDataTable(strSql, para);
           foreach (DataRow dr in dt.Rows)
           {
               TASK_QUARTZ_JOBTRIGGER.Add(ToModel(dr));
           }


           return TASK_QUARTZ_JOBTRIGGER;
       }

       /// <summary>
       /// 获取最大序号
       /// </summary>
       /// <returns></returns>
       public int MaxXh()
       {
           int xh = 0;
           string strSql = "select max(xh) xh from TASK_QUARTZ_JOBTRIGGER";
           DataTable dt = DalBase.ExecuteDataTable(strSql, null);
           if (!string.IsNullOrWhiteSpace(Convert.ToString(dt.Rows[0][0])))
           {
               xh = int.Parse(Convert.ToString(dt.Rows[0][0]));
           }
           xh++;
           return xh;
       }
       /// <summary>
       /// 更新valid字段
       /// </summary>
       /// <param name="nguid"></param>
       /// <param name="valid">是否有效 0:无效 1：有效</param>
       /// <returns></returns>
       public int UpdateValid(string nguid,string valid)
       {
           string strSql = "update TASK_QUARTZ_JOBTRIGGER set valid = :valid where nguid = :nguid ";
           OracleParameter[] para = {
                                         new OracleParameter(":valid",OracleType.Char,1),
                                         new OracleParameter(":nguid",OracleType.VarChar,200)
                                     };
           para[0].Value = valid;
           para[1].Value = nguid;

           return DalBase.ExecuteNonQuery(strSql, para);
       }
       /// <summary>
       /// 更新运行状态
       /// </summary>
       /// <param name="nguid"></param>
       /// <param name="RunStatus">1：正在运行中 2：等待下次运行 3：暂停中</param>
       /// <returns></returns>
       public int UpdateRunStatus(string nguid, string RunStatus)
       {
           string strSql = "update TASK_QUARTZ_JOBTRIGGER set RunStatus = :RunStatus,ISCHANGE=:ISCHANGE where nguid = :nguid ";
           OracleParameter[] para = {
                                         new OracleParameter(":RunStatus",OracleType.Char,1),
                                         new OracleParameter(":ISCHANGE",OracleType.Char,1),
                                         new OracleParameter(":nguid",OracleType.VarChar,200)
                                     };
           para[0].Value = RunStatus;
           para[1].Value = "2";
           para[2].Value = nguid;

           return DalBase.ExecuteNonQuery(strSql, para);
       }
       /// <summary>
       /// 更新是否变更
       /// </summary>
       /// <param name="nguid"></param>
       /// <param name="ISChange">1：发生变化 0：未发生变化</param>
       /// <returns></returns>
       public int UpdateISChange(string nguid, string ISChange)
       {
           string strSql = "update TASK_QUARTZ_JOBTRIGGER set ISChange = :ISChange where nguid = :nguid ";
           OracleParameter[] para = {
                                         new OracleParameter(":ISChange",OracleType.Char,1),
                                         new OracleParameter(":nguid",OracleType.VarChar,200)
                                     };
           para[0].Value = ISChange;
           para[1].Value = nguid;

           return DalBase.ExecuteNonQuery(strSql, para);
       }

       private Task_Quartz_JobTriggerModel ToModel(DataRow dr)
       {
           Task_Quartz_JobTriggerModel model = new Task_Quartz_JobTriggerModel();
           model.NGUID = Convert.ToString(dr["NGUID"]);
           model.JOBNAME = Convert.ToString(dr["JOBNAME"]);
           model.JOBGROUPNAME = Convert.ToString(dr["JOBGROUPNAME"]);
           model.TRIGGERNAME = Convert.ToString(dr["TRIGGERNAME"]);
           model.TRIGGERGROUPNAME = Convert.ToString(dr["TRIGGERGROUPNAME"]);
           model.JOBPATH = Convert.ToString(dr["JOBPATH"]);
           model.JOBCRON = Convert.ToString(dr["JOBCRON"]);
           model.JOBJJ = Convert.ToString(dr["JOBJJ"]);
           model.XH = Convert.ToString(dr["XH"]);
           model.RUNSTATUS = Convert.ToString(dr["RUNSTATUS"]);
           model.ISCHANGE = Convert.ToString(dr["ISCHANGE"]);
           model.VALID = Convert.ToString(dr["VALID"]);
           model.JOBSNAME = Convert.ToString(dr["JOBSNAME"]);
           return model;
       }
    }
}
